<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" >
			<view class="wrapper">
				<view class="address-box" @click="checkAddress(item)">
					<text v-if="item.siteDefault==1" class="tag">默认</text>
					<text class="address">{{ item.siteDetails }} {{item.siteNote}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{ item.siteName }}</text>
					<text class="mobile">{{ item.siteTelephone }}</text>
				</view>
			</view>
			<text class="yticon icon-shanchu4" @click="delAddress(item.aid)"></text>
			<text class="yticon icon-bianji" @click="editAddress(item.aid)"></text>
		</view>
		<button class="add-btn" @click="addAddress">新增地址</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			addressList: [],
			ownerId: ''
		};
	},
	onShow(option) {
		//获取当前业主的id
		this.ownerId = this.$store.state.userInfo.id ; //  this.$store.state.userInfo.ownerId;    由于还没做登录操作，所以只好模拟一个业主编号
		this.getData(this.ownerId);
	},
	methods: {
		//请求数据加载到地址List里面
		getData(ownerId) {
			uni.request({
				url: this.baseurl+'address/userAllAddress/' + ownerId ,
				success: res => {
					if(res.data.code==200){
							//console.log(res.data.data)
						this.addressList = res.data.data;
					}
				}
			});
		},	
		//选择地址
		checkAddress(item){
				//this.$api.prePage()获取上一页实例，在App.vue定义
				this.$api.prePage().addressData=item; 
				this.$api.prePage().longitude=item.lon;
				this.$api.prePage().latitude=item.lat;
				this.$api.prePage().g_aid=item.aid;
				this.$api.prePage().istoNewPage=true;
				
				//这个  addressData就是上一页的收货信息数据，在这里修改
				// 返回上一页并且修改数据：返回修改前一页面数据，修改的是  addressData 这个数据
				 uni.navigateBack()
				//history.back();//这个的作用也是返回上一页

		},
		
		
		//删除地址
		delAddress(addressId) {
			uni.showModal({
				title: '地址',
				content: '确定要删除选择的地址吗？',
				cancelText: '取消',
				confirmText: '确认删除',
				success: res => {
					//cancelText: '取消',confirmText: '确认删除',res.confirm判断，确认之后就会执行下面的
					if (res.confirm) {
						uni.request({

							url: this.baseurl+'address/deleteAddress/' + addressId,
							method: 'DELETE',
							success: res => {
								if (res.data.code == 200) {
									
									this.$api.msg(res.data.message);
									setTimeout(() => {
										//重新加载收货信息
										this.getData(this.ownerId);
									}, 800);
								} else {
									this.$api.msg(res.data.message);
									setTimeout(() => {
										//重新加载收货信息
										this.getData(this.ownerId);
									}, 800);
								}
							}
						});
					}
				}
			});
		},
		
		//编辑地址
		editAddress(addressId) {
			uni.navigateTo({
				//直接传对象
				url: 'addressEdit?addressId=' + addressId
			});
		},

		//新增地址
		addAddress() {
			uni.navigateTo({
				url: 'addressManage'
			});
		}
	}
};
</script>

<style lang="scss">
page {
	padding-bottom: 120upx;
}
.content {
	position: relative;
}
.list {
	display: flex;
	align-items: center;
	padding: 20upx 30upx;
	background: #fff;
	position: relative;
}
.wrapper {
	display: flex;
	flex-direction: column;
	flex: 1;
}
.address-box {
	display: flex;
	align-items: center;
	.tag {
		font-size: 24upx;
		color: $base-color;
		margin-right: 10upx;
		background: #fffafb;
		border: 1px solid #ffb4c7;
		border-radius: 4upx;
		padding: 4upx 10upx;
		line-height: 1;
	}
	.address {
		font-size: 30upx;
		color: $font-color-dark;
	}
}
.u-box {
	font-size: 28upx;
	color: $font-color-light;
	margin-top: 16upx;
	.name {
		margin-right: 30upx;
	}
}
.icon-bianji {
	display: flex;
	align-items: center;
	height: 80upx;
	font-size: 40upx;
	color: $font-color-light;
	padding-left: 30upx;
}

.add-btn {
	position: fixed;
	left: 30upx;
	right: 30upx;
	bottom: 16upx;
	z-index: 95;
	display: flex;
	align-items: center;
	justify-content: center;
	width: 690upx;
	height: 80upx;
	font-size: 32upx;
	color: #fff;
	background-color: $base-color;
	border-radius: 10upx;
	box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
}
</style>
